﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
using Emgu.CV;
using System.Windows;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;

namespace SoGame.Helper
{
    public class ImageHelper
    {
        public ImageHelper() { }

        [DllImport("gdi32")]
        private static extern int DeleteObject(IntPtr o);

        public static BitmapSource ToBitmapSource(IImage image)
        {
            using (System.Drawing.Bitmap source = image.Bitmap)
            {
                IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap

                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    ptr,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                DeleteObject(ptr); //release the HBitmap
                return bs;
            }
        }

        public static byte[] ConvertImageBMPToByteArray(BitmapSource imageToConvert)
        {
            byte[] buffer;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    var encoder = new BmpBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(imageToConvert));
                    encoder.Save(ms);
                    buffer = ms.ToArray();
                }
            }
            catch (Exception) { throw; }

            return buffer;
        }

        public static byte[] ConvertImagePNGToByteArray(BitmapSource imageToConvert)
        {
            byte[] buffer;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    var encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(imageToConvert));
                    encoder.Save(ms);
                    buffer = ms.ToArray();
                }
            }
            catch (Exception) { throw; }

            return buffer;
        }

        public static BitmapSource LoadImage(Byte[] imageBytes)
        {
            BitmapSource source = null;

            using (MemoryStream stream = new MemoryStream(imageBytes))
            {
                var decoder = new BmpBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);

                if (decoder.Frames != null && decoder.Frames.Count > 0)
                    source = decoder.Frames[0];
            }

            return source;
        }

        public static Bitmap GetBitmapFromBitmapSource(BitmapSource bSource)
        {
            Bitmap bmp;
            using (MemoryStream ms = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bSource));
                enc.Save(ms);
                bmp = new Bitmap(ms);
            }
            return bmp;
        }
    }
}
